﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Uber.Core.Model;
using Uber.Core.Repository;

namespace Uber.Data
{
    public class ScamRepo : BaseRepo, IScamRepo
    {
        public ScamRepo(IConnectionFactory connFactory) : base(connFactory)
        {
        }

        public int Ban(long scamId)
        {
            using (var conn = new SqlConnection(Cs))
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "banByScamId";
                    cmd.Parameters.Add("id", SqlDbType.BigInt).Value = scamId;
                    conn.Open();

                    return cmd.ExecuteNonQuery();
                }
            }
        }

        public int Delete(long id)
        {
            using (var conn = new SqlConnection(Cs))
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "deleteScam";
                    cmd.Parameters.Add("id", SqlDbType.BigInt).Value = id;
                    conn.Open();

                    return cmd.ExecuteNonQuery();
                }
            }
        }

        public IEnumerable<Scam> GetPage(int page, int pageSize)
        {
            using (var conn = new SqlConnection(Cs))
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "getScam";
                    cmd.Parameters.Add("pageSize", SqlDbType.Int).Value = pageSize;
                    cmd.Parameters.Add("page", SqlDbType.Int).Value = page;
                    conn.Open();

                    using (var dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            var o = new Scam();
                            o.Id = dr.GetInt64(0);
                            o.Ip = dr.GetString(1);
                            o.Content = dr.GetString(2);
                            yield return o;
                        }
                    }
                }
            }
        }

        public int GetCount()
        {
            using (var conn = new SqlConnection(Cs))
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "getScamCount";
                    conn.Open();

                    return (int)cmd.ExecuteScalar();
                }
            }
        }
    }
    
}